package com.iam.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.iam.admin.service.SysUserService;
import com.iam.common.core.constant.ErrorCodes;
import com.iam.common.core.result.R;
import com.iam.common.core.util.MsgUtils;
import com.iam.security.util.SecurityUtils;
import com.iam4cloud.admin.api.dto.UserInfo;
import com.iam4cloud.admin.api.entity.SysUser;
import com.iam4cloud.admin.api.vo.UserInfoVO;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 八月
 * @description 用户管理
 * @date 2022/11/18
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/user")
@Tag(name = "用户管理模块")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class UserController {
    private final SysUserService userService;

    /**
     * 获取当前用户全部信息
     * @return 用户信息
     */
    @GetMapping(value = { "/info" })
    public R<UserInfoVO> info() {
        String username = SecurityUtils.getUser().getUsername();
        SysUser user = userService.getOne(Wrappers.<SysUser>query().lambda().eq(SysUser::getUsername, username));
        if (user == null) {
            return R.failed(MsgUtils.getMessage(ErrorCodes.SYS_USER_QUERY_ERROR));
        }
        UserInfo userInfo = userService.getUserInfo(user);
        UserInfoVO vo = new UserInfoVO();
        vo.setSysUser(userInfo.getSysUser());
        vo.setRoles(userInfo.getRoles());
        vo.setPermissions(userInfo.getPermissions());
        return R.ok(vo);
    }
}
